"pytest定义前置"
import time
from Method import SlideCode,Tkinter_UI
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from Config import ConfigFile
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.webdriver import Service
#执行输入测试数据
dialog = Tkinter_UI.InputDialog()
dialog.run()
WebUrl=dialog.web_url
Agent_account=dialog.agent_account
Agent_password=dialog.agent_password
Unit_account=dialog.unit_account
Unit_password=dialog.unit_password
mark=dialog.mark
select=dialog.select
@pytest.fixture(scope='session')
def Agent_login():
    """
    代理商的前置登录方法
    ConfigFile配置文件获取驱动路劲
    ConfigFile.chromePath谷歌驱动地址
    ConfigFile.fireFox火狐驱动地址
    ConfigFile.WebUrl自动化启动地址
    定义登录前置功能
    :return:driver实例

    """

    driver=None
    if ConfigFile.browser=="fireFox":
        driver=webdriver.Firefox(service=Service(executable_path = ConfigFile.fireFoxPath))
    elif ConfigFile.browser=="ie":
        driver=webdriver.Ie(service=Service(executable_path = ConfigFile.iePath))
    elif ConfigFile.browser=="chrome":
        driver=webdriver.Chrome()
        # options = Options()
        # options.add_argument("--incognito")  # 启动无痕模式
        # # options.add_argument('ignore-certificate-errors')  #添加忽略不安全
        # driver = webdriver.Chrome(
        #     service=Service(executable_path=ConfigFile.chromePath),
        #     options=options  # 将无痕模式选项传入
        # )
    else:
        print(f"配置文件：{ConfigFile.browser}未配置浏览器驱动")
    driver.maximize_window()    #浏览器最大化
    driver.implicitly_wait(5)    #静默等待最大10s
    driver.get(WebUrl)
    if "中文" in driver.page_source:
        driver.find_element(By.XPATH,'//span[text()="中文"]').click()    #切换中文，为海外自动化需要
    # driver.find_element(By.XPATH,'//input[@type="text"]').send_keys(ConfigFile.account)  #输入账号
    driver.find_element(By.XPATH,'//input[@type="text"]').send_keys(Agent_account)  #输入账号
    # driver.find_element(By.XPATH,'//input[@type="password"]').send_keys(ConfigFile.password)  #输入密码
    driver.find_element(By.XPATH,'//input[@type="password"]').send_keys(Agent_password)  #输入密码
    driver.find_element(By.XPATH,'//span[text()="登录"]').click() #点击登录
    SlideCode.main(driver)    #滑验证码
    try:
        assert "我的用户" in driver.find_element(By.XPATH,'//div[@class="leftboxcl"]').text
        print(f'登录账号：{Agent_account}密码{Agent_password}成功')
        yield driver
        driver.quit()
    except Exception as log:
        print(f'未登录成功，错误日志为：{log}')
        driver.quit()
@pytest.fixture(scope='session')
def Unit_login():
    """
    代理商的前置登录方法
    ConfigFile配置文件获取驱动路劲
    ConfigFile.chromePath谷歌驱动地址
    ConfigFile.fireFox火狐驱动地址
    ConfigFile.WebUrl自动化启动地址
    定义登录前置功能
    :return:driver实例

    """

    driver=None
    if ConfigFile.browser=="fireFox":
        driver=webdriver.Firefox(service=Service(executable_path = ConfigFile.fireFoxPath))
    elif ConfigFile.browser=="ie":
        driver=webdriver.Ie(service=Service(executable_path = ConfigFile.iePath))
    elif ConfigFile.browser=="chrome":
        driver=webdriver.Chrome()
        # options = Options()
        # options.add_argument("--incognito")  # 启动无痕模式
        # options.add_argument('ignore-certificate-errors')  #添加忽略不安全
        # driver = webdriver.Chrome(
        #     service=Service(executable_path=ConfigFile.chromePath),
        #     options=options  # 将无痕模式选项传入
        # )
    else:
        print(f"配置文件：{ConfigFile.browser}未配置浏览器驱动")
    driver.maximize_window()    #浏览器最大化
    driver.implicitly_wait(5)    #静默等待最大10s
    driver.get(WebUrl)
    if "中文" in driver.page_source:
        driver.find_element(By.XPATH,'//span[text()="中文"]').click()    #切换中文，为海外自动化需要
    driver.find_element(By.XPATH,'//span[text()="单位"]').click()
    # driver.find_element(By.XPATH,('//input[@type="text"]')).send_keys(ConfigFile.unit_account)  #输入账号
    driver.find_element(By.XPATH,('//input[@type="text"]')).send_keys(Unit_account)  #输入账号
    # driver.find_element(By.XPATH,('//input[@type="password"]')).send_keys(ConfigFile.unit_password)  #输入密码
    driver.find_element(By.XPATH,('//input[@type="password"]')).send_keys(Unit_password)  #输入密码
    driver.find_element(By.XPATH,('//span[text()="登录"]')).click() #点击登录
    SlideCode.main(driver)    #滑验证码
    try:
        assert "我的用户" in driver.find_element(By.XPATH,('//div[@class="leftboxcl"]')).text
        print(f'登录单位账号：{Unit_account}单位密码：{Unit_password}成功')
        yield driver
        driver.quit()
    except Exception as log:
        print(f'未登录成功，错误日志为：{log}')
        driver.quit()
if __name__ == '__main__':
    Agent_login()